package cn.com.surker.base.erp.service.impl;

import cn.com.surker.base.erp.service.ControllerEepTableService;
import cn.com.surker.common.Code;
import cn.com.surker.database.JDBCTemplateCache;
import cn.com.surker.system.dao.DataSourceInfoDao;
import cn.com.surker.system.entity.DataSourceInfo;
import cn.com.surker.util.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author lucky
 * @create 2024年07月10 8:35
 * @desc
 */
@Service
public class ControllerEepTableServiceImpl implements ControllerEepTableService {

    @Resource
    private DataSourceInfoDao dsDao;

    /**
     * 物流删除erp数据
     * @param orgId
     * @param erpId
     * @param tableName
     * @return
     */
    public Integer deleteTableById(Long orgId, Long erpId, String tableName) {
        if (StringUtils.isAllParamNull(orgId, erpId, tableName))
            return Code.PARAM_EMPTY.getCode();

        DataSourceInfo ds = dsDao.queryDSByOriginId(orgId);
        if (ds == null || ds.getStatus() == 1)
            return Code.ERROR_DATA.getCode();
        Date nowDate = new Date();
        String sql = "update " + tableName + " set IsValid = 0 where id = " + erpId;
        try {
            JdbcTemplate jdbcTemplate = JDBCTemplateCache.getLocalJdbcTemplate(ds);

            Integer code = jdbcTemplate.update(sql);
            if (code > 0)
                return 0;
            return -1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
